package com.errorfinding.rabbitmq.seven;

import com.errorfinding.rabbitmq.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 * 主题交换机模式 topic
 */
public class ReceiveLogsTopic02 {

    private static final String exchange_name = "topic_logs";

    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建信道
        Channel channel = RabbitMqUtils.getChannel();
        //声明交换机
        /**
         * 参数1。交换机名称
         * 参数2。交换机类型/模式
         */
        channel.exchangeDeclare(exchange_name,"topic");
        //声明队列
        String queueName = "Q2";
        /**
         * 参数1.队列名称
         * 参数2.是否持久化
         * 参数3.是否共享
         * 参数4.是否自动删除
         * 参数5.其他参数
         */
        channel.queueDeclare(queueName,false,false,false,null);
        //绑定
        channel.queueBind(queueName,exchange_name,"*.*.rabbit");
        channel.queueBind(queueName,exchange_name,"lazy.#");
        System.out.println("02等待接收消息。。。。。");

        //接收消息
        DeliverCallback deliverCallback  = (tag,message)->{
            System.out.println(new String(message.getBody()));
            System.out.println("接收队列："+queueName+" 绑定键："+message.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(queueName,true,deliverCallback,(tag)->{});

    }
}
